/*
 * Copyright (c) 2006-2025, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-09-27     RT-Thread    first version
 */

#include <rtthread.h>
#include <easyflash.h>
#include <fal.h>
#include <stdlib.h>

#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

#define DEFT_CONFIG_JSON "{\"emeter\":1,\"flwmeter\":2,\"stripsw\":0}"
char sub_dev_config[] = DEFT_CONFIG_JSON;

static const ef_env config_env_set[] = {
    {"sub_dev_type_cfg", "0"},
    {"mdbus_flw_id_cfg", "0"},

};

static EfErrCode init_easyflash_db()
{
    char *p_sub_dev_config;
    EfErrCode init_res = EF_ENV_INIT_FAILED;
    init_res = easyflash_init();
    if (init_res != EF_NO_ERR)
    {
        return init_res;
    }
    else
    {
        p_sub_dev_config = ef_get_env("sub_dev_type_cfg");
        if (p_sub_dev_config == NULL)
        {
            ef_set_env("sub_dev_type_cfg", DEFT_CONFIG_JSON);
            ef_save_env();
        }

    }
    return init_res;
}


int main(void)
{
    char * db_sub_dev_cfg = NULL;
    EfErrCode db_init;
    LOG_D("================systerm started!=====================");

    fal_init();
    db_init = init_easyflash_db();

    if(db_init == EF_NO_ERR){

        db_sub_dev_cfg = ef_get_env("sub_dev_type_cfg");
        LOG_D("===============================================");
        LOG_D("sub dev configration: %s ", db_sub_dev_cfg);
        LOG_D("===============================================");
    }



    return RT_EOK;
}
//
//static void test_env(void)
//{
//    uint32_t i_boot_times = 0;
//    char *c_old_boot_times, c_new_boot_times[11] = {0};
//
//    /* 从环境变量中获取启动次数 */
//    c_old_boot_times = ef_get_env("boot_times");
//    /* 获取启动次数是否失败 */
//    if (c_old_boot_times == RT_NULL)
//        c_old_boot_times[0] = '0';
//
//    i_boot_times = atol(c_old_boot_times);
//    /* 启动次数加 1 */
//    i_boot_times++;
//    LOG_D("===============================================");
//    LOG_D("The system now boot %ld times", i_boot_times);
//    LOG_D("===============================================");
//    /* 数字转字符串 */
//    sprintf(c_new_boot_times, "%ld", i_boot_times);
//    /* 保存开机次数的值 */
//    ef_set_env("boot_times", c_new_boot_times);
//    ef_save_env();
//}
